package com.ebupt.migu.music.autoTri.Controller;

import com.ebupt.migu.music.autoTri.Service.EffectTotalService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ExcelUtils;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.common.util.excelSheet.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author tz
 */
@RestController
@RequestMapping("/effectTotal")
public class EffectTotalController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private EffectTotalService effectTotalService;

    private static String[] smsArr = {"createTime", "date", "tacId", "tacName","policyType","creatorName","triggerScene","success","pullNewCnt","pullNewRate","pullOldCnt","pullOldRate","intersectionUser","mbUser","iopUser","totalFailure","blackUser","repetitionUser","fail","err"};
    private static String[][] smsHead ={
            {"策略创建时间", "策略执行时间", "策略ID", "策略名称","执行类型","创建人","场景","成功下发数","拉新用户数","拉新率","促活用户数","促活率","人群规模","人群规模","人群规模","失败人数","失败人数","失败人数","失败人数","失败人数"},
            {"策略创建时间", "策略执行时间", "策略ID", "策略名称","执行类型","创建人","场景","成功下发数","拉新用户数","拉新率","促活用户数","促活率","交集人群规模","目标人群规模","IOP人群规模","总数","黑名单","去重","调端口失败","调端口异常"}
    };
    private static String[] pushArr = {"createTime", "date", "tacId", "tacName","pushTitle","pushContent","creatorName","updateTime","policyType","triggerScene","msgTotal", "totalSend", "iosSend", "androidSend", "totalDisplayed", "iosDisplayed", "androidDisplayed", "totalClicked", "iosClicked", "androidClicked", "totalCrv", "iosCrv", "androidCrv","intersectionUser","mbUser","iopUser","totalFailure","blackUser","repetitionUser","fail","err"};
    private static String[][] pushHead = {
            {"策略创建时间", "策略执行时间", "策略ID", "策略名称","PUSH标题","PUSH内容","创建人","数据更新时间","执行类型","场景","百日联网用户数", "成功下发数", "成功下发数", "成功下发数", "展示数", "展示数", "展示数", "点击数", "点击数", "点击数", "转化率", "转化率", "转化率","人群规模","人群规模","人群规模","失败人数","失败人数","失败人数","失败人数","失败人数" },
            {"策略创建时间", "策略执行时间", "策略ID", "策略名称","PUSH标题","PUSH内容","创建人","数据更新时间","执行类型","场景","百日联网用户数", "总数", "IOS", "Android", "总数", "IOS", "Android", "总数", "IOS", "Android", "总数", "IOS", "Android","交集人群规模","目标人群规模","IOP人群规模","总数","黑名单","去重","下发异常","下发失败" }};

    private static String[] popupArr = {"date", "vie_pv", "vie_uv", "click_pv","click_uv","success"};
    private static String[] popupHead = {"策略执行日期", "曝光pv", "曝光uv", "点击pv","点击uv","转化人数"};

    private static String[] msgArr = { "createTime", "date", "tacId","msgText","creatorName", "tacName","policyType","triggerScene", "successNum", "singleRead", "singleUnread","crv", "flowPv", "flowUv", "noticePv", "noticeUv","intersectionUser","mbUser","iopUser","totalFailure","blackUser","repetitionUser","fail","err"};
    private static String[][] msgHead = {
            {"策略创建时间", "策略执行时间", "策略ID","消息通知-文字","创建人", "策略名称","执行类型","场景","成功下发数", "单条已读数", "单条未读数","转化率","落地页PV", "落地页UV", "通知页PV", "通知页UV","人群规模","人群规模","人群规模","失败人数","失败人数","失败人数","失败人数","失败人数" },
            {"策略创建时间", "策略执行时间", "策略ID","消息通知-文字","创建人", "策略名称","执行类型","场景","成功下发数", "单条已读数", "单条未读数","转化率","落地页PV", "落地页UV", "通知页PV", "通知页UV","交集人群规模","目标人群规模","IOP人群规模","总数","黑名单","去重","下发异常","下发失败" }
    };
    /**
     * 查询效果统计
     * @return
     */
    @RequestMapping("/qryList")
    public ResultObject qryList( ){
        Map<String,Object> param = ReqestParamUtil.getParams(request);
        return effectTotalService.qryList(param);
    }

    /**
     * 查询echart
     * @return
     */
    @RequestMapping("/qrySum")
    public ResultObject qrySum(){
        Map<String,Object> param = ReqestParamUtil.getParams(request);
        return effectTotalService.qrySum(param);
    }

    /**
     * 导出效果统计
     * @return
     */
    @RequestMapping(value = "/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void export(){
        Map<String,Object> params = ReqestParamUtil.getParams(request);
        List<Map<String, Object>> list = effectTotalService.export(params);
        ExcelUtils excel = new ExcelUtils();
        Sheet sheet = new Sheet();
        // 策略类型
        String tacMode = params.get("tacMode").toString();
        switch (tacMode) {
            case "1":
                excel.setName("sms_total").setVersion("2007");
                sheet.setName("短信效果统计");
                sheet.setHead(smsHead);
                sheet.fillData(list, smsArr);
                break;
            case "2":
                excel.setName("push_total").setVersion("2007");
                sheet.setName("push效果统计");
                sheet.setHead(pushHead);
                sheet.fillData(list, pushArr);
                break;
            case "3":
                excel.setName("msg_total").setVersion("2007");
                sheet.setName("消息通知效果统计");
                sheet.setHead(msgHead);
                sheet.fillData(list, msgArr);
                break;
            default:
                break;
        }
        excel.addSheet(sheet);
        excel.zipAndResponse(response, request);
    }

}
